﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SkillSystem
{
    public class DamageOpportunityPorcesser
    {
        public int CurTimeIndex = 0;

        private float curTime;

        private ISkillTransmitter transmitter;

        private DamageTimeArgs args;

        private Action OnDamage;

        private DamageOpportunityType lastOpportunityType;
        public DamageOpportunityPorcesser(ISkillTransmitter transmitter)
        {
            this.transmitter = transmitter;
        }

        public void Init(Action onDamage)
        {
            DamageOpportunityType curOpportunityType = (DamageOpportunityType)transmitter.CurSkillItem.CurSkillItemData.DamageOpportunityType;
            if (lastOpportunityType == curOpportunityType)
            {
                return;
            }
            OnDamage = null;
            OnDamage += onDamage;
            switch (curOpportunityType)
            {
                case DamageOpportunityType.FromEffect:
                    InitFromEffect();
                    lastOpportunityType = DamageOpportunityType.FromEffect;
                    break;
                case DamageOpportunityType.FromTime:
                    InitFromTime();
                    lastOpportunityType = DamageOpportunityType.FromTime;
                    break;
                default:
                    break;
            }
        }

        #region 时机由特效触发
        private void InitFromEffect()
        {
            if (transmitter.SkillProcessers.ContainsKey(SkillProcesserType.Effect))
            {
                SkillEffectProcesser processer = (SkillEffectProcesser)transmitter.SkillProcessers[SkillProcesserType.Effect];

                processer.RegisterDamageEvent(DoDamage);
            }
            else
            {
                DebugUtils.LogError("未注册Effect类型处理器");
            }
        }
        #endregion

        #region 时机由时间提供
        private void InitFromTime()
        {
            args = new DamageTimeArgs();

            if (transmitter.SkillProcessers.ContainsKey(SkillProcesserType.Time))
            {
                SkillTimeProcesser processer = (SkillTimeProcesser)transmitter.SkillProcessers[SkillProcesserType.Time];
                if (args.TimePoints.Count > 0)
                {
                    processer.OnUpdate += OnUpdate;
                }
                else
                {
                    processer.OnUpdate += (deltaTime) => { DoDamage(); };
                }

                processer.OnItemCompleted += OnEnd;
            }
            else
            {
                DebugUtils.LogError("未注册Time类型处理器");
            }
        }

        void OnUpdate(float deltaTime)
        {
            curTime += deltaTime;

            if (args.TimePoints.Count > CurTimeIndex ) 
            {
                if (curTime >= args.TimePoints[CurTimeIndex])
                {
                    DoDamage();
                    CurTimeIndex++;
                }
            }
        }

        void DoDamage()
        {
            if (OnDamage != null)
            {
                OnDamage();
            }
        }

        void OnEnd()
        {
            curTime = 0;
            CurTimeIndex = 0;
        }
    }
#endregion
}
